package com.s10.ecloud.web.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.s10.ecloud.web.Constants;
import com.s10.ecloud.web.model.User;
import com.s10.ecloud.web.service.ILoginService;

public class LoginAction extends ActionSupport {
	@Resource
	private ILoginService loginService = null;

	private String email;
	private String password;

	/**
	 * @return the mail
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param mail
	 *            the mail to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/** 登录页面跳转请求 */
	@Action(value = "/tologinPage", results = { @Result(name = "input", location = "/page/login.jsp") })
	public String tologinPage() throws Exception {
		// this.shubetu = 1;
		return INPUT;
	}

	/** 退出登录请求 */
	@Action(value = "/exitUser", results = { @Result(name = "input", location = "/page/login.jsp") })
	public String exitUser() throws Exception {
		Object userObj = ActionContext.getContext().getSession()
				.get(Constants.SESSION_KEY_USER);
		if (userObj != null && "".equals(((User)userObj).getEmail()) == false) {
			this.setEmail(((User)userObj).getEmail());
		}
		ActionContext.getContext().getSession()
				.remove(Constants.SESSION_KEY_USER);
		return INPUT;
	}

	/** 处理登录请求 */
	@Action(value = "/login", results = {
			@Result(name = "input", location = "/page/login.jsp"),
			@Result(name = "sys", location = "/page/sys/index.jsp"),
			@Result(name = "ent", location = "/page/ent/index.jsp"),
			@Result(name = "ind", location = "/page/ind/index.jsp") })
	public String login() {
		boolean isSuccess;

		// 输入项目是否为空的check
		isSuccess = this.checkArgs();
		User user = new User();
		// 用户名密码是否正确的check
		if (isSuccess) {
			user = loginService.getUserByMail(email, password);
			if (user == null || "".equals(user.getEmail())) {
				this.addFieldError("pwd", "邮件名或密码不正确");
				isSuccess = false;
			} else if (user.getState() != Constants.USER_ACTIVE_STATE_VALID) {
				this.addFieldError("pwd", "用户还没激活，请先激活，或等待审批");
				isSuccess = false;
			} else {
				ActionContext.getContext().getSession()
						.put(Constants.SESSION_KEY_USER, user);
			}
		}

		// 验证通过时
		if (isSuccess) {
			// 系统用户的时候
			if (user.getRole().getId() == Constants.USER_TYPE_SYS) {
				return "sys";
			}
			// 企业用户的时候
			if (user.getRole().getId() == Constants.USER_TYPE_ENT) {
				return "ent";
				// 个人用户的时候
			} else {
				return "ind";
			}
		}

		return INPUT;
	}

	/** 登录页面跳转请求 */
	@Action(value = "/editPwd", results = { @Result(name = "input", location = "/page/common/editPassword.jsp") })
	public String editPwd() throws Exception {
		return INPUT;
	}

	private boolean checkArgs() {
		boolean result = true;

		if (email == null || email.trim().length() == 0) {
			this.addFieldError("account", "邮件名不能为空");
			result = false;
		}
		if (password == null || password.trim().length() == 0) {
			this.addFieldError("password", "密码不能为空");
			result = false;
		}

		return result;
	}

}
